package com.rickpan.controller;

import com.rickpan.dto.response.SearchSuggestionResponse;
import com.rickpan.dto.response.SearchSuggestionsWrapperResponse;
import com.rickpan.service.SearchService;
import com.rickpan.utils.ResponseUtils;
import com.rickpan.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索控制器
 *
 * @author RickPan Team
 * @since 2025-07-03
 */
@RestController
@RequestMapping("/api/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 获取搜索建议
     *
     * @param q     搜索关键词
     * @param limit 返回数量限制，默认8个
     * @return 搜索建议列表
     */
    @GetMapping("/suggestions")
    public ResponseEntity<?> getSearchSuggestions(
            @RequestParam String q,
            @RequestParam(defaultValue = "8") Integer limit) {

        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            // System.out.println("获取搜索建议: userId=" + currentUserId + ", keyword=" + q + ", limit=" + limit);

            List<SearchSuggestionResponse> suggestions = searchService.getSearchSuggestions(currentUserId, q, limit);

            // 包装返回数据
            SearchSuggestionsWrapperResponse wrapper = new SearchSuggestionsWrapperResponse(
                    suggestions,
                    suggestions.size(), // 暂时使用当前结果数量作为总数
                    false // 暂时设置为没有更多结果
            );

            return ResponseUtils.success(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("获取搜索建议失败: " + e.getMessage());
        }
    }

    /**
     * 执行完整搜索
     *
     * @param q        搜索关键词
     * @param page     页码，默认0
     * @param size     每页大小，默认20
     * @param fileType 文件类型筛选
     * @return 搜索结果
     */
    @GetMapping("")
    public ResponseEntity<?> search(
            @RequestParam String q,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) String fileType) {

        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            // System.out.println("执行搜索: userId=" + currentUserId + ", keyword=" + q);

            // TODO: 实现完整搜索逻辑
            return ResponseUtils.success("搜索功能开发中...");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("搜索失败: " + e.getMessage());
        }
    }

    /**
     * 获取搜索历史
     *
     * @param limit 返回数量限制，默认10个
     * @return 搜索历史列表
     */
    @GetMapping("/history")
    public ResponseEntity<?> getSearchHistory(@RequestParam(defaultValue = "10") Integer limit) {

        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            // System.out.println("获取搜索历史: userId=" + currentUserId + ", limit=" + limit);

            // TODO: 实现搜索历史逻辑
            return ResponseUtils.success("搜索历史功能开发中...");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("获取搜索历史失败: " + e.getMessage());
        }
    }

    /**
     * 保存搜索历史
     *
     * @param keyword 搜索关键词
     * @return 操作结果
     */
    @PostMapping("/history")
    public ResponseEntity<?> saveSearchHistory(@RequestParam String keyword) {

        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            // System.out.println("保存搜索历史: userId=" + currentUserId + ", keyword=" + keyword);

            // TODO: 实现保存搜索历史逻辑
            return ResponseUtils.success("搜索历史保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("保存搜索历史失败: " + e.getMessage());
        }
    }

    /**
     * 清除搜索历史
     *
     * @return 操作结果
     */
    @DeleteMapping("/history")
    public ResponseEntity<?> clearSearchHistory() {

        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            // System.out.println("清除搜索历史: userId=" + currentUserId);

            // TODO: 实现清除搜索历史逻辑
            return ResponseUtils.success("搜索历史清除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("清除搜索历史失败: " + e.getMessage());
        }
    }

    /**
     * 获取热门搜索
     *
     * @param limit 返回数量限制，默认5个
     * @return 热门搜索列表
     */
    @GetMapping("/popular")
    public ResponseEntity<?> getPopularSearches(@RequestParam(defaultValue = "5") Integer limit) {

        try {
            // System.out.println("获取热门搜索: limit=" + limit);

            // TODO: 实现热门搜索逻辑
            return ResponseUtils.success("热门搜索功能开发中...");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("获取热门搜索失败: " + e.getMessage());
        }
    }
}
